<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog title="零件导入" v-model="props.visible" @close="handleVisible('cancel')">
    <div class="query">
      <el-form size="small" label-width="120">
        <el-form-item label="机型：" required>
          <el-select v-model="query.modelNumber" @change="modelChange" clearable>
            <el-option v-for="item in modelNumberList" :key="item.name" :label="item.name" :value="item.mark"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次号：" required>
          <el-select v-model="query.batchNumber" @change="batchChange" clearable>
            <el-option v-for="item in batchList" :key="item.name" :label="item.name" :value="item.mark"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部段：" required>
          <el-select v-model="query.sectionNumber" @change="sectionChange" clearable>
            <el-option v-for="item in sectionList" :key="item.name" :label="item.name" :value="item.mark"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组件：" required>
          <el-select v-model="query.componentNumber" @change="conponentChange" clearable>
            <el-option v-for="item in componentList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="零件BDF文件：" required>
          <el-upload
            :on-change="handleChange"
            :on-remove="handleRemove"
            :file-list="fileList"
            :show-file-list="true"
            :auto-upload="false"
            :multiple="false"
            action="#"
          >
            <el-button size="small" type="primary">选取文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button :disabled="isClick" size="small" type="primary" @click="handleVisible('submit')">确 定</el-button>
        <el-button size="small" @click="handleVisible('cancel')">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
  <script name="ImportPart" setup>
import { getStructureTreeList, getComponentBySection, importPart, checkImportSinglePart } from '@/api/elementsLibrary';

const props = defineProps(['visible']);
const emits = defineEmits(['handleEvent']);
const query = ref({
  modelNumber: '',
  batchNumber: '',
  sectionNumber: '',
  componentNumber: ''
});
const selectList = ref([]);
const modelNumberList = ref([]);
const batchList = ref([]);
const sectionList = ref([]);
const componentList = ref([]);
const fileList = ref([]);
const submitFile = ref(null);
const isClick = ref(false);
const getStructureList = () => {
  getStructureTreeList({pageSize:9999}).then((res) => {
    if (res) {
      selectList.value = res.rows;
      modelNumberList.value = res?.rows?.filter((el) => {
        return el.parentId === 0;
      });
    }
  });
};
const modelChange = (e) => {
  query.value.batchNumber = "";
  query.value.sectionNumber = "";
  query.value.componentNumber = "";
  batchList.value = [];
  sectionList.value = [];
  componentList.value = [];
  const parentId = selectList.value.filter((el) => {
    return el.mark === e;
  });
  batchList.value = selectList.value.filter((el) => {
    return el.parentId === parentId[0]?.id;
  });
};
const batchChange = (e) => {
  query.value.sectionNumber = "";
  query.value.componentNumber = "";
  sectionList.value = [];
  componentList.value = [];
  const parentId = selectList.value.filter((el) => {
    return el.mark === e;                                                                                                                                                                                                                
  });                                                                                                                                                                                                                                      
  sectionList.value = selectList.value.filter((el) => {                                                                                                                                                                                        
    return el.parentId === parentId[0]?.id;                                                                                                                                                                                                     
  });     
};                                                                                                                                                                                                                                        
const sectionChange = (e) => {
  query.value.componentNumber = "";
  componentList.value = [];
  getComponentBySection({ modelNumber: query.value.modelNumber, batchNumber: query.value.batchNumber, sectionNumber: query.value.sectionNumber }).then((res) => {
    if (res) {
      componentList.value = res?.data;
    }
  });
};
const handleRemove = (file) => {
  submitFile.value = null;
  fileList.value = [];
};
const handleChange = (file,list) => {
  fileList.value = [];
  fileList.value.push(file);
  const fileType = file.name.split('.');
  const length = fileType.length;
  console.log(fileList.value);
  console.log(list)
  if (!(fileType[length-1] === 'bdf')) {
    ElMessage({ showClose: true, message: '文件类型错误,请上传bdf文件', type: 'error' });
    return;
  }
  submitFile.value = file.raw;
};
const handleVisible = (type) => {
  if (type === 'cancel') {
    emits('handleEvent', { type: 'cancel' });
  } else {
    //提交数据
    if (!query.value.modelNumber) {
      ElMessage({ showClose: true, message: '请选择机型！', type: 'error' });
      return;
    }
    if (!query.value.batchNumber) {
      ElMessage({ showClose: true, message: '请选择批次！', type: 'error' });
      return;
    }
    if (!query.value.sectionNumber) {
      ElMessage({ showClose: true, message: '请选择部段！', type: 'error' });
      return;
    }
    if (!query.value.componentNumber) {
      ElMessage({ showClose: true, message: '请选择组件！', type: 'error' });
      return;
    }
    if (!submitFile.value) {
      ElMessage({ showClose: true, message: '请上传bdf文件！', type: 'error' });
      return;
    }
    const formData = new FormData();
    formData.append('file', submitFile.value);
    formData.append('modelNumber', query.value.modelNumber);
    formData.append('sectionNumber', query.value.sectionNumber);
    formData.append('componentNumber', query.value.componentNumber);
    formData.append('batchNumber', query.value.batchNumber);
    // 规则检查
    isClick.value = true;
    checkImportSinglePart(formData).then((res) => {
      if (res) {
        // handleVisible('submit');
        let msgStr = '';
        // msgStr += `该零件版本匹配${res.data.versionCheck == 0 ? '不通过' : '通过'},`
        msgStr += `该零件在原始清单中${res.data.exitCheck == 0 ? '不存在' : '已存在'},`
        msgStr += '是否继续？'

        // emits('handleEvent', { type: 'refresh' });
        // ElMessage({ showClose: true, message: '导入成功', type: 'success' });
        // isClick.value = false;
        ElMessageBox.confirm(msgStr, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          importSinglePart(res.data)
        }).catch(() => {
          isClick.value = false;      
        });
      } else {
        ElMessage({ showClose: true, message: '文件检测失败！', type: 'error' });
        isClick.value = false;
      }
    }).catch(() => {
      isClick.value = false;      
    });
    // importPart(formData).then((res) => {
    //   if (res) {
    //     ElMessage({ showClose: true, message: '文件上传成功！', type: 'success' });
    //     emits('handleEvent', { type: 'refresh' });
    //   }
    // });
  }
};
const importSinglePart = (data) => {
  importPart(data).then((res) => {
    if (res) {
      ElMessage({ showClose: true, message: '文件上传成功！', type: 'success' });
      emits('handleEvent', { type: 'refresh' });
      isClick.value = false;
    }
  }).catch(() => {
    isClick.value = false;      
  });
}

onMounted(() => {
  getStructureList();
});
</script>
  <style lang="scss"coped>
</style>
  